package com.lottery.shadowbd.io.api;

import com.google.gson.Gson;
import com.lottery.shadowbd.bean.ResultBean;
import com.lottery.shadowbd.callback.ECallback;
import com.lottery.shadowbd.support.Constant;
import com.lottery.shadowbd.tuple.Tuple2;
import com.lottery.shadowbd.utils.LogUtils;

import okhttp3.FormBody;

/**
 * Created by Administrator on 2018/3/3 0003.
 */

public class WebApiIO {

    volatile static WebApiIO instance;

    WebApiIO() {

    }

    static public WebApiIO getInstance() {
        if (instance == null) {
            synchronized (WebApiIO.class) {
                if (instance == null) {
                    instance = new WebApiIO();
                }
            }
        }
        return instance;
    }

    /**
     * 用户信息
     *
     * @param appid
     * @param callback
     */
    public void getWebUrl(String appid, final ECallback<ResultBean, Tuple2<Integer, String>> callback) {

        FormBody.Builder builder = new FormBody.Builder()
                .add("appid", appid);

        MainApiIO.getInstance().postRequest(Constant.UrlWeb, builder.build(), new ECallback<String, Tuple2<Integer, String>>() {
            @Override
            public void onSuccess(String res) {
                LogUtils.d("用户信息===onSuccess");
                //JSONObject jsonObject = (JSONObject) res;

                ResultBean userJson = new ResultBean();
                userJson = new Gson().fromJson(res, userJson.getClass());

                if (callback != null) {
                    if (userJson != null) {
                        callback.onSuccess(userJson);
                    } else {
                        callback.onError(new Tuple2<>(0, "返回信息为空"));
                    }
                }
            }

            @Override
            public void onError(Tuple2<Integer, String> error) {
                if (callback != null) {
                    callback.onError(error);
                }
            }

            @Override
            public void onThrowable(Throwable e) {
                if (callback != null) {
                    callback.onThrowable(e);
                }
            }
        });
    }

}
